//-----------------------------------------------------------------------
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------
// MicrosoftAjaxAdoNet.js
// Microsoft ADO.NET Data Service AJAX Framework.

Type.registerNamespace("Sys.Data");

Sys.Data.AdoNetQueryBuilder = function Sys$Data$AdoNetQueryBuilder(uri) {
    /// <param name="uri" type="String"></param>
    var e = Function._validateParams(arguments, [
        {name: "uri", type: String}
    ]);
    if (e) throw e;


    this._queryParameters = {};
    this._uri = uri;

    var idxQuery = uri.indexOf('?');
    if (idxQuery >= 0) {
                this._uri = uri.substr(0, idxQuery);
        var params = uri.substr(idxQuery + 1).split('&');
        for (var i in params) {
            param = params[i];
            var idxValue = param.indexOf('=');
            if (idxValue >= 0) {
                this._queryParameters[param.substr(0, idxValue)] = param.substr(idxValue + 1);
            }
            else {
                this._queryParameters[param] = "";
            }
        }
    }
}














    function Sys$Data$AdoNetQueryBuilder$get_skip() {
        /// <value type="Number" integer="true" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._getIntParam("$skip");
    }
    function Sys$Data$AdoNetQueryBuilder$set_skip(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number, mayBeNull: true, integer: true}]);
        if (e) throw e;

        this._setParam("$skip", value);
    }

    function Sys$Data$AdoNetQueryBuilder$get_top() {
        /// <value type="Number" integer="true" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._getIntParam("$top");
    }
    function Sys$Data$AdoNetQueryBuilder$set_top(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number, mayBeNull: true, integer: true}]);
        if (e) throw e;

        this._setParam("$top", value);
    }

    function Sys$Data$AdoNetQueryBuilder$get_orderby() {
        /// <value type="String" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._getStringParam("$orderby");
    }
    function Sys$Data$AdoNetQueryBuilder$set_orderby(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String, mayBeNull: true}]);
        if (e) throw e;

        this._setParam("$orderby", value);
    }

    function Sys$Data$AdoNetQueryBuilder$get_filter() {
        /// <value type="String" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._getStringParam("$filter");
    }
    function Sys$Data$AdoNetQueryBuilder$set_filter(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String, mayBeNull: true}]);
        if (e) throw e;

        this._setParam("$filter", value);
    }

    function Sys$Data$AdoNetQueryBuilder$get_expand() {
        /// <value type="String" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._getStringParam("$expand");
    }
    function Sys$Data$AdoNetQueryBuilder$set_expand(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String, mayBeNull: true}]);
        if (e) throw e;

        this._setParam("$expand", value);
    }

    function Sys$Data$AdoNetQueryBuilder$get_resourcePath() {
        /// <value type="string"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._uri;
    }






    function Sys$Data$AdoNetQueryBuilder$get_queryParameters() {
        /// <value type="Object"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._queryParameters;
    }





    function Sys$Data$AdoNetQueryBuilder$toString() {
        /// <returns type="string"></returns>
        if (arguments.length !== 0) throw Error.parameterCount();
        var params = new Array();
        for (var key in this._queryParameters) {
            if (!Array.contains(Sys.Data.AdoNetQueryBuilder._queryOptions, key)) {
                var value = this._queryParameters[key];
                if (value != null) {
                    Array.add(params, { key: key, value: value });
                }
            }
        }
                for (var i in Sys.Data.AdoNetQueryBuilder._queryOptions) {
            var key = Sys.Data.AdoNetQueryBuilder._queryOptions[i];
            var value = this._queryParameters[key];
            if (value != null) {
                Array.add(params, { key: key, value: value });
            }
        }

        var sb = new Sys.StringBuilder(this._uri);
        var firstElement = true;
        for (var i in params) {
            sb.append((firstElement) ? '?' : '&');
            sb.append(params[i].key);
            sb.append('=');
            sb.append(params[i].value);
            firstElement = false;
        }
        return sb.toString();
    }





    function Sys$Data$AdoNetQueryBuilder$_getIntParam(name) {
        var value = parseInt(this._queryParameters[name]);
        return (isNaN(value)) ? null : value;
    }

    function Sys$Data$AdoNetQueryBuilder$_getStringParam(name) {
        var value = this._queryParameters[name];
        return value || null;
    }

    function Sys$Data$AdoNetQueryBuilder$_setParam(name, value) {
        if (value == null) {
            delete this._queryParameters[name];
        } else {
            this._queryParameters[name] = value;
        }
    }

Sys.Data.AdoNetQueryBuilder.prototype = {

            
    _queryParameters: null,
    _uri: null,

            
    get_skip: Sys$Data$AdoNetQueryBuilder$get_skip,
    set_skip: Sys$Data$AdoNetQueryBuilder$set_skip,

    get_top: Sys$Data$AdoNetQueryBuilder$get_top,
    set_top: Sys$Data$AdoNetQueryBuilder$set_top,

    get_orderby: Sys$Data$AdoNetQueryBuilder$get_orderby,
    set_orderby: Sys$Data$AdoNetQueryBuilder$set_orderby,

    get_filter: Sys$Data$AdoNetQueryBuilder$get_filter,
    set_filter: Sys$Data$AdoNetQueryBuilder$set_filter,

    get_expand: Sys$Data$AdoNetQueryBuilder$get_expand,
    set_expand: Sys$Data$AdoNetQueryBuilder$set_expand,

    get_resourcePath: Sys$Data$AdoNetQueryBuilder$get_resourcePath,
    

    get_queryParameters: Sys$Data$AdoNetQueryBuilder$get_queryParameters,

            
    toString: Sys$Data$AdoNetQueryBuilder$toString,

            
    _getIntParam: Sys$Data$AdoNetQueryBuilder$_getIntParam,
    
    _getStringParam: Sys$Data$AdoNetQueryBuilder$_getStringParam,

    _setParam: Sys$Data$AdoNetQueryBuilder$_setParam

}


Sys.Data.AdoNetQueryBuilder._queryOptions = new Array("$filter", "$orderby", "$skip", "$top");

Sys.Data.AdoNetQueryBuilder.registerClass("Sys.Data.AdoNetQueryBuilder");
Sys.Data._AdoNetUtil = function Sys$Data$_AdoNetUtil() {
        throw Error.invalidOperation();
}

Sys.Data._AdoNetUtil.concatUris = function Sys$Data$_AdoNetUtil$concatUris(serviceUri, resourceUri) {
    if (resourceUri.indexOf("//") >= 0) {
                return resourceUri;
    }
    
    if (serviceUri.endsWith('/')) {
        serviceUri = serviceUri.substr(0, serviceUri.length - 1);
    }
    if (resourceUri.startsWith('/')) {
        resourceUri = resourceUri.substr(1);
    }
    return serviceUri + '/' + resourceUri;
}

Sys.Data._AdoNetUtil.extractETag = function Sys$Data$_AdoNetUtil$extractETag(item) {
            return (item.__metadata) ? (item.__metadata.etag || null) : null;
}

Sys.Data._AdoNetUtil.extractUri = function Sys$Data$_AdoNetUtil$extractUri(item) {
            return (item.__metadata) ? (item.__metadata.uri || null) : null;
}

Sys.Data._AdoNetUtil.registerClass("Sys.Data._AdoNetUtil");    
Sys.Data.AdoNetActionResult = function Sys$Data$AdoNetActionResult(result, error, actionContext, operation) {
    /// <param name="result" mayBeNull="true"></param>
    /// <param name="error" type="Sys.Data.AdoNetServiceError" mayBeNull="true"></param>
    /// <param name="actionContext" mayBeNull="true"></param>
    /// <param name="operation" type="String"></param>
    var e = Function._validateParams(arguments, [
        {name: "result", mayBeNull: true},
        {name: "error", type: Sys.Data.AdoNetServiceError, mayBeNull: true},
        {name: "actionContext", mayBeNull: true},
        {name: "operation", type: String}
    ]);
    if (e) throw e;

    this._result = result;
    this._error = error;
    this._actionContext = actionContext;
    this._operation = operation;
}
















    function Sys$Data$AdoNetActionResult$get_actionContext() {
        /// <value type="Object" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._actionContext;
    }

    function Sys$Data$AdoNetActionResult$get_error() {
        /// <value type="Sys.Data.AdoNetServiceError" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._error;
    }

    function Sys$Data$AdoNetActionResult$get_operation() {
        /// <value type="String"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._operation;
    }

    function Sys$Data$AdoNetActionResult$get_result() {
        /// <value type="Object" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._result;
    }

Sys.Data.AdoNetActionResult.prototype = {

            
    _actionContext: null,
    _error: null,
    _operation: null,
    _result: null,
    
            
    get_actionContext: Sys$Data$AdoNetActionResult$get_actionContext,

    get_error: Sys$Data$AdoNetActionResult$get_error,

    get_operation: Sys$Data$AdoNetActionResult$get_operation,
    
    get_result: Sys$Data$AdoNetActionResult$get_result

}

Sys.Data.AdoNetActionResult.registerClass("Sys.Data.AdoNetActionResult");
Sys.Data.AdoNetActionSequence = function Sys$Data$AdoNetActionSequence(dataService) {
    /// <param name="dataService" type="Sys.Data.AdoNetServiceProxy"></param>
    var e = Function._validateParams(arguments, [
        {name: "dataService", type: Sys.Data.AdoNetServiceProxy}
    ]);
    if (e) throw e;


    this._actionQueue = new Array();
    this._dataService = dataService;
}














    function Sys$Data$AdoNetActionSequence$get_serviceProxy() {
        /// <value type="Sys.Data.AdoNetServiceProxy" mayBeNull="false"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._dataService;
    }





    function Sys$Data$AdoNetActionSequence$addInsertAction(item, resourceSetUri, actionContext) {
        /// <param name="item" type="Object"></param>
        /// <param name="resourceSetUri" type="String" mayBeNull="true"></param>
        /// <param name="actionContext" mayBeNull="true" optional="true"></param>
        var e = Function._validateParams(arguments, [
            {name: "item", type: Object},
            {name: "resourceSetUri", type: String, mayBeNull: true},
            {name: "actionContext", mayBeNull: true, optional: true}
        ]);
        if (e) throw e;


        var dataService = this._dataService;
        Array.enqueue(this._actionQueue,
            function(o) {
                dataService.insert(
                    item,
                    resourceSetUri,
                    Sys.Data.AdoNetActionSequence._genSuccessCallback(o),
                    Sys.Data.AdoNetActionSequence._genFailureCallback(o),
                    actionContext 
                )
            }
        );
    }

    function Sys$Data$AdoNetActionSequence$addInvokeAction(operationUri, httpVerb, parameters, actionContext) {
        /// <param name="operationUri" type="String"></param>
        /// <param name="httpVerb" type="String" mayBeNull="true" optional="true"></param>
        /// <param name="parameters" type="Object" mayBeNull="true" optional="true"></param>
        /// <param name="actionContext" mayBeNull="true" optional="true"></param>
        var e = Function._validateParams(arguments, [
            {name: "operationUri", type: String},
            {name: "httpVerb", type: String, mayBeNull: true, optional: true},
            {name: "parameters", type: Object, mayBeNull: true, optional: true},
            {name: "actionContext", mayBeNull: true, optional: true}
        ]);
        if (e) throw e;


        var dataService = this._dataService;
        Array.enqueue(this._actionQueue,
            function(o) {
                dataService.invoke(
                    operationUri,
                    httpVerb,
                    parameters,
                    Sys.Data.AdoNetActionSequence._genSuccessCallback(o),
                    Sys.Data.AdoNetActionSequence._genFailureCallback(o),
                    actionContext 
                )
            }
        );
    }

    function Sys$Data$AdoNetActionSequence$addUpdateAction(item, actionContext) {
        /// <param name="item" type="Object"></param>
        /// <param name="actionContext" mayBeNull="true" optional="true"></param>
        var e = Function._validateParams(arguments, [
            {name: "item", type: Object},
            {name: "actionContext", mayBeNull: true, optional: true}
        ]);
        if (e) throw e;


        var dataService = this._dataService;
        Array.enqueue(this._actionQueue,
            function(o) {
                dataService.update(
                    item,
                    Sys.Data.AdoNetActionSequence._genSuccessCallback(o),
                    Sys.Data.AdoNetActionSequence._genFailureCallback(o),
                    actionContext 
                )
            }
        );
    }

    function Sys$Data$AdoNetActionSequence$addRemoveAction(item, actionContext) {
        /// <param name="item" type="Object" mayBeNull="true"></param>
        /// <param name="actionContext" mayBeNull="true" optional="true"></param>
        var e = Function._validateParams(arguments, [
            {name: "item", type: Object, mayBeNull: true},
            {name: "actionContext", mayBeNull: true, optional: true}
        ]);
        if (e) throw e;


        var dataService = this._dataService;
        Array.enqueue(this._actionQueue,
            function(o) {
                dataService.remove(
                    item,
                    Sys.Data.AdoNetActionSequence._genSuccessCallback(o),
                    Sys.Data.AdoNetActionSequence._genFailureCallback(o),
                    actionContext 
                )
            }
        );
    }

    function Sys$Data$AdoNetActionSequence$clearActions() {
        if (arguments.length !== 0) throw Error.parameterCount();
        Array.clear(this._actionQueue);
    }

    function Sys$Data$AdoNetActionSequence$execute(actionsCallback, userContext) {
        /// <param name="actionsCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="userContext" mayBeNull="true" optional="true"></param>
        var e = Function._validateParams(arguments, [
            {name: "actionsCallback", type: Function, mayBeNull: true, optional: true},
            {name: "userContext", mayBeNull: true, optional: true}
        ]);
        if (e) throw e;


                var o = {
            actionResultQueue: new Array(),
            hasError: false,
            remainingActions: this._actionQueue
        };

                this._actionQueue = new Array();

                Array.enqueue(o.remainingActions, function(o) {
            if (actionsCallback) {
                window.setTimeout(function() { actionsCallback(o.actionResultQueue, o.hasError, userContext); }, 0);
            }
        });
        Array.dequeue(o.remainingActions)(o);
    }

Sys.Data.AdoNetActionSequence.prototype = {

            
    _actionQueue: null,
    _dataService: null,

            
    get_serviceProxy: Sys$Data$AdoNetActionSequence$get_serviceProxy,

            
    addInsertAction: Sys$Data$AdoNetActionSequence$addInsertAction,

    addInvokeAction: Sys$Data$AdoNetActionSequence$addInvokeAction,

    addUpdateAction: Sys$Data$AdoNetActionSequence$addUpdateAction,

    addRemoveAction: Sys$Data$AdoNetActionSequence$addRemoveAction,

    clearActions: Sys$Data$AdoNetActionSequence$clearActions,

    execute: Sys$Data$AdoNetActionSequence$execute

}


Sys.Data.AdoNetActionSequence._genSuccessCallback = function Sys$Data$AdoNetActionSequence$_genSuccessCallback(o) {
    return function(result, actionContext, operation) {
        var newAR = new Sys.Data.AdoNetActionResult(result, null, actionContext, operation);
        Array.enqueue(o.actionResultQueue, newAR);
        Array.dequeue(o.remainingActions)(o);     };
};

Sys.Data.AdoNetActionSequence._genFailureCallback = function Sys$Data$AdoNetActionSequence$_genFailureCallback(o) {
    return function(error, actionContext, operation) {
        o.hasError = true;
        var newAR = new Sys.Data.AdoNetActionResult(null, error, actionContext, operation);
        Array.enqueue(o.actionResultQueue, newAR);
        Array.dequeue(o.remainingActions)(o);     };
};

Sys.Data.AdoNetActionSequence.registerClass("Sys.Data.AdoNetActionSequence");

Sys.Data.AdoNetInvokeParametersBuilder = function Sys$Data$AdoNetInvokeParametersBuilder() {
    if (arguments.length !== 0) throw Error.parameterCount();
    
    this._queryBuilder = new Sys.Data.AdoNetQueryBuilder("");
    this._parameters = this._queryBuilder.get_queryParameters();
}














    function Sys$Data$AdoNetInvokeParametersBuilder$get_parameters() {
        /// <value></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._parameters;
    }





    function Sys$Data$AdoNetInvokeParametersBuilder$addBoolean(name, value) {
        /// <param name="name" type="String"></param>
        /// <param name="value" type="Boolean"></param>
        var e = Function._validateParams(arguments, [
            {name: "name", type: String},
            {name: "value", type: Boolean}
        ]);
        if (e) throw e;


        this._parameters[name] = value.toString();
    }

    function Sys$Data$AdoNetInvokeParametersBuilder$addDate(name, value, includeTimeZone) {
        /// <param name="name" type="String"></param>
        /// <param name="value" type="Date"></param>
        /// <param name="includeTimeZone" type="Boolean" optional="true"></param>
        var e = Function._validateParams(arguments, [
            {name: "name", type: String},
            {name: "value", type: Date},
            {name: "includeTimeZone", type: Boolean, optional: true}
        ]);
        if (e) throw e;


                        var converted = (includeTimeZone)
            ? value.format("yyyy-MM-ddTHH:mm:ss.fffffffzzz")
            : value.format("yyyy-MM-ddTHH:mm:ss.fffffff");
            
        this._parameters[name] = "datetime'" + converted + "'";
    }

    function Sys$Data$AdoNetInvokeParametersBuilder$addDecimal(name, value) {
        /// <param name="name" type="String"></param>
        /// <param name="value" type="Number"></param>
        var e = Function._validateParams(arguments, [
            {name: "name", type: String},
            {name: "value", type: Number}
        ]);
        if (e) throw e;


        this._parameters[name] = value.toString() + "M";
    }

    function Sys$Data$AdoNetInvokeParametersBuilder$addDouble(name, value) {
        /// <param name="name" type="String"></param>
        /// <param name="value" type="Number"></param>
        var e = Function._validateParams(arguments, [
            {name: "name", type: String},
            {name: "value", type: Number}
        ]);
        if (e) throw e;


        this._parameters[name] = value.toString();
    }

    function Sys$Data$AdoNetInvokeParametersBuilder$addGuid(name, value) {
        /// <param name="name" type="String"></param>
        /// <param name="value" type="String"></param>
        var e = Function._validateParams(arguments, [
            {name: "name", type: String},
            {name: "value", type: String}
        ]);
        if (e) throw e;


                var matchFound = value.match(Sys.Data.AdoNetInvokeParametersBuilder._guidRegex);
        if (!matchFound) {
            var e = Error.create(Sys.Data.AdoNetRes.invalidGuid, { name: "Sys.Data.AdoNetException" });
            throw e;
        }
        
        this._parameters[name] = "guid'" + value + "'";
    }

    function Sys$Data$AdoNetInvokeParametersBuilder$addInteger(name, value) {
        /// <param name="name" type="String"></param>
        /// <param name="value" type="Number" integer="true"></param>
        var e = Function._validateParams(arguments, [
            {name: "name", type: String},
            {name: "value", type: Number, integer: true}
        ]);
        if (e) throw e;


        this._parameters[name] = value.toString();
    }

    function Sys$Data$AdoNetInvokeParametersBuilder$addString(name, value) {
        /// <param name="name" type="String"></param>
        /// <param name="value" type="String"></param>
        var e = Function._validateParams(arguments, [
            {name: "name", type: String},
            {name: "value", type: String}
        ]);
        if (e) throw e;


                this._parameters[name] = "'" + value.replace(new RegExp("'", "g"), "''") + "'";
    }

    function Sys$Data$AdoNetInvokeParametersBuilder$toString() {
        /// <returns type="String"></returns>
        if (arguments.length !== 0) throw Error.parameterCount();

        return this._queryBuilder.toString();
    }

Sys.Data.AdoNetInvokeParametersBuilder.prototype = {

            
    _parameters: null,
    _queryBuilder: null,

            
    get_parameters: Sys$Data$AdoNetInvokeParametersBuilder$get_parameters,

            
    addBoolean: Sys$Data$AdoNetInvokeParametersBuilder$addBoolean,

    addDate: Sys$Data$AdoNetInvokeParametersBuilder$addDate,

    addDecimal: Sys$Data$AdoNetInvokeParametersBuilder$addDecimal,

    addDouble: Sys$Data$AdoNetInvokeParametersBuilder$addDouble,

    addGuid: Sys$Data$AdoNetInvokeParametersBuilder$addGuid,

    addInteger: Sys$Data$AdoNetInvokeParametersBuilder$addInteger,

    addString: Sys$Data$AdoNetInvokeParametersBuilder$addString,

    toString: Sys$Data$AdoNetInvokeParametersBuilder$toString

}


Sys.Data.AdoNetInvokeParametersBuilder._guidRegex = /^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$/;

Sys.Data.AdoNetInvokeParametersBuilder.registerClass("Sys.Data.AdoNetInvokeParametersBuilder");
Sys.Data.AdoNetRes = {

            
    "invalidGuid": "The provided value must be of the form 'dddddddd-dddd-dddd-dddd-dddddddddddd', where each d is a digit or a character A - F.",
    "propertyNotFound": "The property '{0}' was not found or did not contain an associated metadata URI.",
    
            
    "operationFailed": "The data operation '{0}' failed.",
    "operationTimedOut": "The data operation '{0}' timed out.",
    "serviceVersionTooHigh": "The URI '{0}' points to an ADO.NET Data Service of a higher version than is supported by this library.",
    "uriNotAdoNetService": "The URI '{0}' does not point to an ADO.NET Data Service."
    
}
Sys.Data.AdoNetServiceError = function Sys$Data$AdoNetServiceError(timedOut, message) {
    /// <param name="timedOut" type="Boolean"></param>
    /// <param name="message" type="String"></param>
    var e = Function._validateParams(arguments, [
        {name: "timedOut", type: Boolean},
        {name: "message", type: String}
    ]);
    if (e) throw e;

    this._timedOut = timedOut;
    this._message = message;
}
















    function Sys$Data$AdoNetServiceError$get_errorObject() {
        /// <value type="Object" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._errorObject;
    }

    function Sys$Data$AdoNetServiceError$get_message() {
        /// <value type="String" mayBeNull="false"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._message;
    }

    function Sys$Data$AdoNetServiceError$get_statusCode() {
        /// <value type="Number" integer="true" mayBeNull="false"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._statusCode;
    }

    function Sys$Data$AdoNetServiceError$get_timedOut() {
        /// <value type="Boolean" mayBeNull="false"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._timedOut;
    }

Sys.Data.AdoNetServiceError.prototype = {

                
    _errorObject: null,
    _message: null,
    _statusCode: -1,
    _timedOut: false,

                
    get_errorObject: Sys$Data$AdoNetServiceError$get_errorObject,
    
    get_message: Sys$Data$AdoNetServiceError$get_message,
    
    get_statusCode: Sys$Data$AdoNetServiceError$get_statusCode,
    
    get_timedOut: Sys$Data$AdoNetServiceError$get_timedOut
    
}

Sys.Data.AdoNetServiceError.registerClass("Sys.Data.AdoNetServiceError");    
Sys.Data.AdoNetServiceProxy = function Sys$Data$AdoNetServiceProxy(serviceUri) {
    /// <param name="serviceUri" type="String"></param>
    var e = Function._validateParams(arguments, [
        {name: "serviceUri", type: String}
    ]);
    if (e) throw e;

    this._serviceUri = serviceUri;
}



















    function Sys$Data$AdoNetServiceProxy$get_defaultFailedCallback() {
        /// <value type="Function" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._defaultFailedCallback;
    }
    function Sys$Data$AdoNetServiceProxy$set_defaultFailedCallback(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Function, mayBeNull: true}]);
        if (e) throw e;

        this._defaultFailedCallback = value;
    }

    function Sys$Data$AdoNetServiceProxy$get_defaultSucceededCallback() {
        /// <value type="Function" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._defaultSucceededCallback;
    }
    function Sys$Data$AdoNetServiceProxy$set_defaultSucceededCallback(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Function, mayBeNull: true}]);
        if (e) throw e;

        this._defaultSucceededCallback = value;
    }

    function Sys$Data$AdoNetServiceProxy$get_defaultUserContext() {
        /// <value type="Object" mayBeNull="true"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this.defaultUserContext;
    }
    function Sys$Data$AdoNetServiceProxy$set_defaultUserContext(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Object, mayBeNull: true}]);
        if (e) throw e;

        this.defaultUserContext = value;
    }

    function Sys$Data$AdoNetServiceProxy$get_replaceOnUpdate() {
        /// <value type="Boolean" mayBeNull="false"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._replaceOnUpdate;
    }
    function Sys$Data$AdoNetServiceProxy$set_replaceOnUpdate(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Boolean}]);
        if (e) throw e;

        this._replaceOnUpdate = value;
    }

    function Sys$Data$AdoNetServiceProxy$get_serviceUri() {
        /// <value type="String" mayBeNull="false"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._serviceUri;
    }

    function Sys$Data$AdoNetServiceProxy$get_timeout() {
        /// <value type="Number" integer="true" mayBeNull="false"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._timeout || Sys.Net.WebRequestManager.get_defaultTimeout();
    }
    function Sys$Data$AdoNetServiceProxy$set_timeout(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Number, integer: true}]);
        if (e) throw e;

        this._timeout = value;
    }





    function Sys$Data$AdoNetServiceProxy$createActionSequence() {
        /// <returns type="Sys.Data.AdoNetActionSequence"></returns>
        if (arguments.length !== 0) throw Error.parameterCount();
        return new Sys.Data.AdoNetActionSequence(this);
    }

    function Sys$Data$AdoNetServiceProxy$insert(item, resourceSetUri, succeededCallback, failedCallback, userContext, webRequest) {
        /// <param name="item" type="Object"></param>
        /// <param name="resourceSetUri" type="String"></param>
        /// <param name="succeededCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="failedCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="userContext" mayBeNull="true" optional="true"></param>
        /// <param name="webRequest" type="Sys.Net.WebRequest" mayBeNull="true" optional="true"></param>
        /// <returns type="Sys.Net.WebRequest"></returns>
        var e = Function._validateParams(arguments, [
            {name: "item", type: Object},
            {name: "resourceSetUri", type: String},
            {name: "succeededCallback", type: Function, mayBeNull: true, optional: true},
            {name: "failedCallback", type: Function, mayBeNull: true, optional: true},
            {name: "userContext", mayBeNull: true, optional: true},
            {name: "webRequest", type: Sys.Net.WebRequest, mayBeNull: true, optional: true}
        ]);
        if (e) throw e;


        var wRequest = this._prepareWebRequest(item, resourceSetUri, "POST", succeededCallback, failedCallback, userContext, "insert", webRequest);
        wRequest.invoke();
        return wRequest;
    }

    function Sys$Data$AdoNetServiceProxy$invoke(operationUri, httpVerb, parameters, succeededCallback, failedCallback, userContext, webRequest) {
        /// <param name="operationUri" type="String"></param>
        /// <param name="httpVerb" type="String" mayBeNull="true" optional="true"></param>
        /// <param name="parameters" type="Object" mayBeNull="true" optional="true"></param>
        /// <param name="succeededCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="failedCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="userContext" mayBeNull="true" optional="true"></param>
        /// <param name="webRequest" type="Sys.Net.WebRequest" mayBeNull="true" optional="true"></param>
        /// <returns type="Sys.Net.WebRequest"></returns>
        var e = Function._validateParams(arguments, [
            {name: "operationUri", type: String},
            {name: "httpVerb", type: String, mayBeNull: true, optional: true},
            {name: "parameters", type: Object, mayBeNull: true, optional: true},
            {name: "succeededCallback", type: Function, mayBeNull: true, optional: true},
            {name: "failedCallback", type: Function, mayBeNull: true, optional: true},
            {name: "userContext", mayBeNull: true, optional: true},
            {name: "webRequest", type: Sys.Net.WebRequest, mayBeNull: true, optional: true}
        ]);
        if (e) throw e;

        
                var qb = new Sys.Data.AdoNetQueryBuilder(operationUri);
        parameters = parameters || {};
        for (key in parameters) {
            qb.get_queryParameters()[encodeURIComponent(key)] = encodeURIComponent(parameters[key]);
        }
        
        httpVerb = httpVerb || "GET";
        var wRequest = this._prepareWebRequest(null, qb.toString(), httpVerb, succeededCallback, failedCallback, userContext, operationUri, webRequest);
        wRequest.invoke();
        return wRequest;       
    }

    function Sys$Data$AdoNetServiceProxy$populateDeferredProperty(item, property, succeededCallback, failedCallback, userContext, webRequest) {
        /// <param name="item" type="Object"></param>
        /// <param name="property" type="String"></param>
        /// <param name="succeededCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="failedCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="userContext" mayBeNull="true" optional="true"></param>
        /// <param name="webRequest" type="Sys.Net.WebRequest" mayBeNull="true" optional="true"></param>
        /// <returns type="Sys.Net.WebRequest"></returns>
        var e = Function._validateParams(arguments, [
            {name: "item", type: Object},
            {name: "property", type: String},
            {name: "succeededCallback", type: Function, mayBeNull: true, optional: true},
            {name: "failedCallback", type: Function, mayBeNull: true, optional: true},
            {name: "userContext", mayBeNull: true, optional: true},
            {name: "webRequest", type: Sys.Net.WebRequest, mayBeNull: true, optional: true}
        ]);
        if (e) throw e;

        
        var succeededHelper = Function.createDelegate(this, function(result, context, operation) {
                        item[property] = result;
            var callback = succeededCallback || this._defaultSucceededCallback;
            if (callback) {
                callback(item, context, operation);
            }
        });
        
        var uri;
        if (item[property] && item[property].__deferred && item[property].__deferred.uri) {
            uri = item[property].__deferred.uri;
        }
        else if (item.__metadata && item.__metadata.uri) {
            uri = item.__metadata.uri + '/' + property;
        }
        else {
                        var message = String.format(Sys.Data.AdoNetRes.propertyNotFound, property);
            throw Sys.Data.AdoNetServiceProxy._createFailedError(property, message);
        }
        
        var wRequest = this._prepareWebRequest(null, uri, "GET", succeededHelper, failedCallback, userContext, property, webRequest);
        wRequest.invoke();
        return wRequest;
    }

    function Sys$Data$AdoNetServiceProxy$query(query, succeededCallback, failedCallback, userContext, webRequest) {
        /// <param name="query" type="String" mayBeNull="true" optional="true"></param>
        /// <param name="succeededCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="failedCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="userContext" mayBeNull="true" optional="true"></param>
        /// <param name="webRequest" type="Sys.Net.WebRequest" mayBeNull="true" optional="true"></param>
        /// <returns type="Sys.Net.WebRequest"></returns>
        var e = Function._validateParams(arguments, [
            {name: "query", type: String, mayBeNull: true, optional: true},
            {name: "succeededCallback", type: Function, mayBeNull: true, optional: true},
            {name: "failedCallback", type: Function, mayBeNull: true, optional: true},
            {name: "userContext", mayBeNull: true, optional: true},
            {name: "webRequest", type: Sys.Net.WebRequest, mayBeNull: true, optional: true}
        ]);
        if (e) throw e;


        var wRequest = this._prepareWebRequest(null, query, "GET", succeededCallback, failedCallback, userContext, query, webRequest);
        wRequest.invoke();
        return wRequest;
    }

    function Sys$Data$AdoNetServiceProxy$update(item, succeededCallback, failedCallback, userContext, webRequest) {
        /// <param name="item" type="Object"></param>
        /// <param name="succeededCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="failedCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="userContext" mayBeNull="true" optional="true"></param>
        /// <param name="webRequest" type="Sys.Net.WebRequest" mayBeNull="true" optional="true"></param>
        /// <returns type="Sys.Net.WebRequest"></returns>
        var e = Function._validateParams(arguments, [
            {name: "item", type: Object},
            {name: "succeededCallback", type: Function, mayBeNull: true, optional: true},
            {name: "failedCallback", type: Function, mayBeNull: true, optional: true},
            {name: "userContext", mayBeNull: true, optional: true},
            {name: "webRequest", type: Sys.Net.WebRequest, mayBeNull: true, optional: true}
        ]);
        if (e) throw e;


        var verb = (this._replaceOnUpdate) ? "PUT" : "MERGE";
        var wRequest = this._prepareWebRequest(item, null, verb, succeededCallback, failedCallback, userContext, "update", webRequest);
        wRequest.invoke();
        return wRequest;
    }

    function Sys$Data$AdoNetServiceProxy$remove(item, succeededCallback, failedCallback, userContext, webRequest) {
        /// <param name="item" type="Object"></param>
        /// <param name="succeededCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="failedCallback" type="Function" mayBeNull="true" optional="true"></param>
        /// <param name="userContext" mayBeNull="true" optional="true"></param>
        /// <param name="webRequest" type="Sys.Net.WebRequest" mayBeNull="true" optional="true"></param>
        /// <returns type="Sys.Net.WebRequest"></returns>
        var e = Function._validateParams(arguments, [
            {name: "item", type: Object},
            {name: "succeededCallback", type: Function, mayBeNull: true, optional: true},
            {name: "failedCallback", type: Function, mayBeNull: true, optional: true},
            {name: "userContext", mayBeNull: true, optional: true},
            {name: "webRequest", type: Sys.Net.WebRequest, mayBeNull: true, optional: true}
        ]);
        if (e) throw e;

        
        var wRequest = this._prepareWebRequest(item, null, "DELETE", succeededCallback, failedCallback, userContext, "remove", webRequest);
        
                wRequest.set_body(null);
        delete wRequest.get_headers()["Content-Type"];
        
        wRequest.invoke();
        return wRequest;
    }





    function Sys$Data$AdoNetServiceProxy$_onResponseComplete(executor, onSuccess, onFailure, userContext, operation) {
                if (!executor.get_responseAvailable()) {
            var wasTimeout = executor.get_timedOut();
            var message = (wasTimeout)
                ? String.format(Sys.Data.AdoNetRes.operationTimedOut, operation)
                : String.format(Sys.Data.AdoNetRes.operationFailed, operation);

            if (onFailure) {
                var dsError = new Sys.Data.AdoNetServiceError(wasTimeout, message);
                onFailure(dsError, userContext, operation);
                return;
            }
            else {
                throw Sys.Data.AdoNetServiceProxy._createFailedError(operation, message);
            }
        }
        
        var statusCode = executor.get_statusCode();
        
                        if (statusCode == 1223 || statusCode == 0) {
            statusCode = 204;
        }

                        var versionHeader = executor.getResponseHeader('DataServiceVersion');
        if (!versionHeader.startsWith('1.0;') && statusCode != 204) {
            var message = (versionHeader.length > 0)
                ? String.format(Sys.Data.AdoNetRes.serviceVersionTooHigh, this._serviceUri)
                : String.format(Sys.Data.AdoNetRes.uriNotAdoNetService, this._serviceUri);

            if (onFailure) {
                var dsError = new Sys.Data.AdoNetServiceError(false , message);
                onFailure(dsError, userContext, operation);
                return;
            }
            else {
                throw Sys.Data.AdoNetServiceProxy._createFailedError(operation, message);
            }
        }
        
                var contentType = executor.getResponseHeader("Content-Type");
        var errorResult = null;
        var objResult = null;

        if (contentType.startsWith("application/json")) {
            var executorResult = executor.get_object();
            errorResult = executorResult.error;
            objResult = executorResult.d;
        }

        if (statusCode < 200 || statusCode >= 300) {
                        var message = (errorResult)
                ? errorResult.message.value
                : String.format(Sys.Data.AdoNetRes.operationFailed, operation);

            if (onFailure) {
                var dsError = new Sys.Data.AdoNetServiceError(false , message);
                dsError._statusCode = statusCode;
                dsError._errorObject = errorResult;
                onFailure(dsError, userContext, operation);
            }
            else {
                throw Sys.Data.AdoNetServiceProxy._createFailedError(operation, message);
            }
        }
        else if (onSuccess) {
            onSuccess(objResult, userContext, operation);
        }
    }

    function Sys$Data$AdoNetServiceProxy$_prepareWebRequest(item, relativeUri, verb, onSuccess, onFailure, context, operation, webRequest) {
                webRequest = webRequest || new Sys.Net.WebRequest();
        webRequest.set_url(Sys.Data._AdoNetUtil.concatUris(this._serviceUri, relativeUri || ""));
        webRequest.set_timeout(this.get_timeout());

        var headers = webRequest.get_headers();
        headers["Accept"] = "application/json";
        headers["DataServiceVersion"] = "1.0;AspNetAjax";
        headers["MaxDataServiceVersion"] = "1.0;";

                webRequest.set_httpVerb(verb);
        if (this._usePostTunneling) {
            var verbUpper = verb.toUpperCase();
            if ((verbUpper == "PUT") || (verbUpper == "DELETE") || (verbUpper == "MERGE")) {
                webRequest.set_httpVerb("POST");
                headers["X-HTTP-Method"] = verbUpper;
            }
        }

                if (item) {
            webRequest.set_body(Sys.Serialization.JavaScriptSerializer.serialize(item));
            headers["Content-Type"] = "application/json";

            var eTag = Sys.Data._AdoNetUtil.extractETag(item);
            if (eTag) {
                headers["If-Match"] = eTag;
            }

            var uri = Sys.Data._AdoNetUtil.extractUri(item);
            if (uri) {
                webRequest.set_url(uri);
            }
        }

                onSuccess = onSuccess || this._defaultSucceededCallback;
        onFailure = onFailure || this._defaultFailedCallback;
        if ((typeof(context) === "undefined") || (context === null)) {
            context = this._defaultUserContext;
        }

        webRequest.add_completed(Function.createDelegate(this, function(executor) {
            this._onResponseComplete(executor, onSuccess, onFailure, context, operation);
        }));
        return webRequest;
    }

Sys.Data.AdoNetServiceProxy.prototype = {

            
    _defaultFailedCallback: null,
    _defaultSucceededCallback: null,
    _defaultUserContext: null,
    _replaceOnUpdate: false,
    _serviceUri: null,
    _timeout: 0,
    _usePostTunneling: true,

            
    get_defaultFailedCallback: Sys$Data$AdoNetServiceProxy$get_defaultFailedCallback,
    set_defaultFailedCallback: Sys$Data$AdoNetServiceProxy$set_defaultFailedCallback,

    get_defaultSucceededCallback: Sys$Data$AdoNetServiceProxy$get_defaultSucceededCallback,
    set_defaultSucceededCallback: Sys$Data$AdoNetServiceProxy$set_defaultSucceededCallback,

    get_defaultUserContext: Sys$Data$AdoNetServiceProxy$get_defaultUserContext,
    set_defaultUserContext: Sys$Data$AdoNetServiceProxy$set_defaultUserContext,

    get_replaceOnUpdate: Sys$Data$AdoNetServiceProxy$get_replaceOnUpdate,
    set_replaceOnUpdate: Sys$Data$AdoNetServiceProxy$set_replaceOnUpdate,

    get_serviceUri: Sys$Data$AdoNetServiceProxy$get_serviceUri,

    get_timeout: Sys$Data$AdoNetServiceProxy$get_timeout,
    set_timeout: Sys$Data$AdoNetServiceProxy$set_timeout,

                
    createActionSequence: Sys$Data$AdoNetServiceProxy$createActionSequence,

    insert: Sys$Data$AdoNetServiceProxy$insert,
    
    invoke: Sys$Data$AdoNetServiceProxy$invoke,
    
    populateDeferredProperty: Sys$Data$AdoNetServiceProxy$populateDeferredProperty,

    query: Sys$Data$AdoNetServiceProxy$query,
    
    update: Sys$Data$AdoNetServiceProxy$update,
    
    remove: Sys$Data$AdoNetServiceProxy$remove,

            
    _onResponseComplete: Sys$Data$AdoNetServiceProxy$_onResponseComplete,

    _prepareWebRequest: Sys$Data$AdoNetServiceProxy$_prepareWebRequest

}


Sys.Data.AdoNetServiceProxy._createFailedError = function Sys$Data$AdoNetServiceProxy$_createFailedError(operation, errorMessage) {
    var displayMessage = "Sys.Data.AdoNetException: " + errorMessage;
    var e = Error.create(displayMessage, { name: "Sys.Data.AdoNetException", operation: operation });
    e.popStackFrame();
    return e;
}

Sys.Data.AdoNetServiceProxy.registerClass("Sys.Data.AdoNetServiceProxy");    
